<?php
/**
 * Object Relational Persistence Mapping Class for table: test_service
 *
 * @package	com.clearhealth
 * @author	ClearHealth Inc.
 */
class TestService extends ORDataObject {

	/**#@+
	 * Fields of table: test_service mapped to class members
	 */
	var $test_service_id		= '';
	var $test_id		= '';
	var $service_id		= '';
	/**#@-*/


	/**
	 * DB Table
	 */
	var $_table = 'test_service';

	/**
	 * Primary Key
	 */
	var $_key = 'test_service_id';
	
	/**
	 * Internal Name
	 */
	var $_internalName = 'TestService';

	/**
	 * Handle instantiation
	 */
	function TestService() {
		parent::ORDataObject();
	}

	public function listTestItem($testId) {
	    $field  =   L10nHelper::changeField(L10nHelper::getLocale(), 'name');
		
		$ds = new Datasource_sql();
		$sql = array (
			'cols'		=> "TS.test_service_id, S.service_id, S.$field name, T.test_id, T.$field test_name",
			'from'		=> "test T 
				INNER JOIN test_service TS ON T.test_id = TS.test_id
				INNER JOIN service S ON (TS.service_id = S.service_id AND S.disabled = 0)",				
			'where'		=> "T.test_id = $testId",
			'orderby'	=> "test_name, name"
			); 
		$ds->setup($this->_db, $sql);
		return $ds->toArray();
	}
	
	public function deleteService($serviceId) {
		$sql = "DELETE FROM test_service WHERE service_id = $serviceId";
		$this->_execute($sql);
	}
	
	public function getTestGroupOfItem($serviceId) {
		$sql = "SELECT test_id FROM $this->_table WHERE service_id = $serviceId";
		$res = $this->_execute($sql);
		if (empty($res)) {
			return 0;
		}
		return $res->fields['test_id'];
	}
	
	public function getTestServiceId($serviceId) {
		$sql = "SELECT test_service_id FROM $this->_table WHERE service_id = $serviceId";
		$res = $this->_execute($sql);
		if (empty($res)) {
			return 0;
		}
		return $res->fields['test_service_id'];
	}
	
    public function getService($testServiceId) {
    	$ds = new Datasource_sql();
        $sql = array (
            'cols'      => "service_id",
            'from'      => "$this->_table",
            'where'     => "test_service_id = '$testServiceId'"
            ); 
        $ds->setup($this->_db, $sql);
        $result = $ds->toArray();
        if (empty($result)) {
        	return null;
        }
        return $result[0];
    }
}
?>